// This is the main DLL file.

#include "VxlEngine.h"
#include "Utils\Globals.h"
#include "Utils\KeyboardHandler.h"
#include "Utils\MouseHandler.h"
#include "Cameras\WASDCamera.h"
//#include "VxlObjects\ParticleSystem\ParticleSystemManager.h"

VxlEngine::VxlEngine(ID3D11Device* device, HWND window, HINSTANCE appInstance, bool useMouse, bool useKeyboard){
	Globals::Device = device;
	device->GetImmediateContext(&Globals::Context);
	Globals::Window = window;
	Globals::AppInstance = appInstance;
	vxls = std::vector<VxlObject*>();
	
	this->usingMouse = useMouse;
	this->usingKeyboard = useKeyboard;
		
	if(useKeyboard)
		Utils::KeyboardHandler::Init();
	if(useMouse)
		Utils::MouseHandler::SetUp(appInstance, window);

	//particleSystemManager = new ParticleSystemManager();

	defaultCamera = new Cameras::WASDCamera();
	defaultCamera->Setup(XMVectorSet(0.0f, 5.0f, 5.0f, 1.0f), XMVectorZero());
	SetCamera(defaultCamera);
}	

VxlEngine::~VxlEngine(void){
		
}

void VxlEngine::CleanUp(){
	for(UINT i = 0; i<vxls.size(); i++)
		delete vxls[i];

	delete defaultCamera;
}

/*ParticleSystemManager* VxlEngine::GetParticleSystemManager(){
	return particleSystemManager;
}*/

void VxlEngine::AddVxl(VxlObject* object){
	vxls.push_back(object);
}

void VxlEngine::Draw(float dt){
	if(vxls.size() == 0)
		return;
	
	//particleSystemManager->Step(dt);

	VxlObject::SetPerFrame();
	for(UINT i = 0; i<vxls.size(); i++)
		vxls[i]->Draw();
	
}

void VxlEngine::Update(float dt){

	if(usingKeyboard)
		Utils::KeyboardHandler::Update();
	if (usingMouse)
		Utils::MouseHandler::DetectInput();

	Globals::ActiveCamera->Update(dt);

	for(UINT i = 0; i<vxls.size(); i++)
		vxls[i]->Update(dt);
}

void VxlEngine::SetCamera(Cameras::BaseCamera *camera){
	Globals::ActiveCamera = camera;
}


